package cn.acewill.yxd.yunxundian.pubClass;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.model.RMain;
import cn.acewill.yxd.yunxundian.model.Schedule;
import cn.acewill.yxd.yunxundian.model.rJson;
import cn.acewill.yxd.yunxundian.model.sType;
import cn.acewill.yxd.yunxundian.model.tempLostRR;
import cn.acewill.yxd.yunxundian.ui.MainActivity;


/**
 * Created by xxx on 2018/1/17.
 */

public class RMainUtils {

    protected YXDAPP yxdapp = new YXDAPP();
    protected DbManager db = x.getDb(yxdapp.getDaoConfig());

    private RMainUtils() {

    }

    private static RMainUtils instance;

    public static RMainUtils getInstance() {
        if (instance == null) {
            instance = new RMainUtils();
        }
        return instance;
    }

    public List<RMain> loadRMins(int schId) {
        try {
            Schedule schedule = db.selector(Schedule.class).where("ID", "=", schId).findFirst();
            sType type = db.selector(sType.class).where("ID", "=", schedule.getSTypeID()).findFirst();
            rJson rmian = db.selector(rJson.class).where("ID", "=", type.getReportId()).findFirst();
            if (rmian == null) {
                return null;
            }
            String json = rmian.getjSon();
            Gson gson = new Gson();
            Type typeList = new TypeToken<ArrayList<RMain>>() {
            }.getType();
            return gson.fromJson(pubClass.convertToArrayJsonNormal(json), typeList);
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    //获取评估表的所有关键项
    public List<RMain.TablesBean.RowsBean.Q2Bean> getPointItem(List<RMain> rMains) {
        List<RMain.TablesBean> tables = rMains.get(0).getTables();
        List<RMain.TablesBean.RowsBean.Q2Bean> q2Beans = new ArrayList<>();
        for (RMain.TablesBean table : tables) {
            List<RMain.TablesBean.RowsBean> rowsBeen = table.getRows();
            for (RMain.TablesBean.RowsBean rows : rowsBeen) {
                // TODO: 2017/9/5 实际设置2与3,此处为了测试
                if (rows.getQ2().getIsPoint() >= 2) {
                    q2Beans.add(rows.getQ2());
                }
            }
        }
        return q2Beans;
    }

    //获取所有关键项的全部数量
    public int getPointItemCount(List<RMain> rMains) {
        List<RMain.TablesBean> tables = rMains.get(0).getTables();
        int count = 0;
        for (RMain.TablesBean table : tables) {
            List<RMain.TablesBean.RowsBean> rowsBeen = table.getRows();
            for (RMain.TablesBean.RowsBean rows : rowsBeen) {
                if (rows.getQ2().getIsPoint() >= 2) {
                    count++;
                }
            }
        }
        return count;
    }


    //获取评估表的所有小项
    public  List<RMain.TablesBean.RowsBean.Q2Bean> getAllQ2Bean(List<RMain> rMains) {
        List<RMain.TablesBean> tables = rMains.get(0).getTables();
        List<RMain.TablesBean.RowsBean.Q2Bean> q2Beans = new ArrayList<>();
        for (RMain.TablesBean table : tables) {
            List<RMain.TablesBean.RowsBean> rowsBeen = table.getRows();
            for (RMain.TablesBean.RowsBean rows : rowsBeen) {
                q2Beans.add(rows.getQ2());
            }
        }
        return q2Beans;
    }

    public int getAllQ2Count(List<RMain> rMains) {
        List<RMain.TablesBean> tables = rMains.get(0).getTables();
        int count = 0;
        for (RMain.TablesBean table : tables) {
            List<RMain.TablesBean.RowsBean> rowsBeen = table.getRows();
            for (RMain.TablesBean.RowsBean rows : rowsBeen) {
                count++;
            }
        }
        return count;
    }

    //获取当前大项的所有关键项
    public String getPointCountByMainId(List<RMain> rMains, int mainId, int schId) {
        int count = 0;
        int writeCount = 0;
        List<RMain.TablesBean> tables = rMains.get(0).getTables();
        List<RMain.TablesBean.RowsBean> rowsBeens = new ArrayList<>();
        List<RMain.TablesBean.RowsBean.Q2Bean> q2Beans = new ArrayList<>();
        for (RMain.TablesBean table : tables) {
            if (table.getQ1().getID() == mainId) {
                rowsBeens = table.getRows();
                break;
            }
        }
        if (rowsBeens != null && rowsBeens.size() > 0) {
            for (RMain.TablesBean.RowsBean row : rowsBeens) {
                if (row.getQ2().getIsPoint() >= 2) {
                    q2Beans.add(row.getQ2());
                    count++;
                }
            }
        }
        if (count > 0) {
            for (RMain.TablesBean.RowsBean.Q2Bean q2 : q2Beans) {
                try {
                    tempLostRR rr = db.selector(tempLostRR.class).where("schId", "=", schId)
                            .and("rRowId", "=", q2.getID()).findFirst();
                    if (rr != null) {
                        writeCount++;
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                }
            }
        }
        return writeCount + "/" + count;
    }

    //小项集合的关键项数据
    public String getPointCountByQ2Beans(List<RMain.TablesBean.RowsBean.Q2Bean> q2Beans, int schId) {
        int count = 0;
        int write = 0;
        if (q2Beans != null && q2Beans.size() > 0) {
            for (RMain.TablesBean.RowsBean.Q2Bean q2Bean : q2Beans) {
                if (q2Bean.getIsPoint() >= 2) {
                    count++;
                    try {
                        tempLostRR rr = db.selector(tempLostRR.class).where("schId", "=", schId)
                                .and("rRowId", "=", q2Bean.getID()).findFirst();
                        if (rr != null) {
                            write++;
                        }
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return write + "/" + count;
    }

    //获取当前评估表编辑的关键项
    public int getAllWritePointCount(List<RMain> rMains, int schId) {
        List<RMain.TablesBean> tables = rMains.get(0).getTables();
        int count = 0;
        for (RMain.TablesBean table : tables) {
            List<RMain.TablesBean.RowsBean> rowsBeen = table.getRows();
            for (RMain.TablesBean.RowsBean rows : rowsBeen) {
                if (rows.getQ2().getIsPoint() >= 2) {
                    try {
                        tempLostRR rr = db.selector(tempLostRR.class).where("schId", "=", schId)
                                .and("rRowId", "=", rows.getQ2().getID()).findFirst();
                        if (rr != null) {
                            count++;
                        }
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return count;
    }

    public RMain.TablesBean.RowsBean getRowByMInId(List<RMain> rMains, int minId) {
        List<RMain.TablesBean> tables = rMains.get(0).getTables();
        for (RMain.TablesBean table : tables) {
            List<RMain.TablesBean.RowsBean> rowsBeens = table.getRows();
            for (RMain.TablesBean.RowsBean row : rowsBeens) {
                if (row.getQ2().getID() == minId) {
                    return row;
                }
            }
        }
        return null;
    }

    public RMain.TablesBean.Q1Bean getQ1ByMInId(List<RMain> rMains, int minId) {
        List<RMain.TablesBean> tables = rMains.get(0).getTables();
        for (RMain.TablesBean table : tables) {
            RMain.TablesBean.Q1Bean q1 = table.getQ1();
            List<RMain.TablesBean.RowsBean> rowsBeens = table.getRows();
            for (RMain.TablesBean.RowsBean row : rowsBeens) {
                if (row.getQ2().getID() == minId) {
                    return q1;
                }
            }
        }
        return null;
    }

    /**
     * 获取小项的最大分数
     * @param rMains rmain
     * @param minId rowid
     * @return score
     */
    public double getMaxScoreByrowId(List<RMain> rMains, int minId) {
        List<RMain.TablesBean> tables = rMains.get(0).getTables();
        for (RMain.TablesBean table : tables) {
            List<RMain.TablesBean.RowsBean> rowsBeens = table.getRows();
            for (RMain.TablesBean.RowsBean row : rowsBeens) {
                if (row.getQ2().getID() == minId) {
                    return row.getQ2().getMaxScore();
                }
            }
        }
        return 0;
    }

    /**
     * 获取当前大项编辑的数量
     * @param rMains
     * @param position 大项的角标
     */
    public int  getWrittCountByQ1(List<RMain> rMains,int position,int schId){
        int count = 0;
        List<RMain.TablesBean.RowsBean> rowlist=rMains.get(0).getTables().get(position).getRows();
        if (rowlist!=null&&rowlist.size()>0){
            for (RMain.TablesBean.RowsBean row:rowlist){
                try {
                    tempLostRR rr = db.selector(tempLostRR.class).where("schId", "=", schId)
                            .and("rRowId", "=", row.getQ2().getID()).findFirst();
                    if (rr!=null){
                        count++;
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                }
            }
        }
        return  count;
    }

    public int getWriteByQ2Beans(int schId,List<RMain.TablesBean.RowsBean.Q2Bean>q2Beans){
        int writeCount = 0;
        if (q2Beans!=null&&q2Beans.size()>0){
            for (RMain.TablesBean.RowsBean.Q2Bean q2Bean:q2Beans){
                try {
                    tempLostRR rr = db.selector(tempLostRR.class).where("schId", "=", schId)
                            .and("rRowId", "=", q2Bean.getID()).findFirst();
                    if (rr!=null){
                        writeCount++;
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                }
            }
        }
        return writeCount;
    }


    /*获取所有正在编辑的item
    *直接查询rr
    */
    public int getAllWriteCount(int schId){
        int count = 0;
        try {
            List<tempLostRR> rrList = db.selector(tempLostRR.class).where("schId", "=", schId)
                    .and("empId", "=", MainActivity.id).findAll();
            if (rrList!=null&&rrList.size()>0){
                count = rrList.size();
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        return count;
    }
    public int getQ2CountByPosition(List<RMain> rMains,int position){
        int count = 0;
        List<RMain.TablesBean.RowsBean> rowlist=rMains.get(0).getTables().get(position).getRows();
        if (rowlist!=null&&rowlist.size()>0) {
            for (RMain.TablesBean.RowsBean row : rowlist) {
                count++;
            }

        }
        return count;
    }

}

